package com.example.taobaounion.presenter.impl;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.bean.SelectContent;
import com.example.taobaounion.model.bean.SelectedPageCategory;
import com.example.taobaounion.presenter.ISelectedPagePresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.RetrofitManager;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.ISelectPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectedPagePresenterImpl implements ISelectedPagePresenter {
    private ISelectPageCallback mViewCallback=null;
    private  Api mApi=null;


    public SelectedPagePresenterImpl (){
        //拿到retrofit
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {
        if(mViewCallback!=null){
            mViewCallback.onLoading();
        }
        //拿到retrofit
        Call<SelectedPageCategory> task = mApi.getSelectedPageCategories();
        task.enqueue(new Callback<SelectedPageCategory>() {
            @Override
            public void onResponse(Call<SelectedPageCategory> call, Response<SelectedPageCategory> response) {
                int code = response.code();
                if(code== HttpURLConnection.HTTP_OK){
                    SelectedPageCategory result = response.body();
                    //通知UI更新
                    if (mViewCallback != null) {
                        mViewCallback.onCategoriesLoaded(result);
                    }

                }else {
                    onloadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedPageCategory> call, Throwable t) {
                LogUtils.d(SelectedPagePresenterImpl.this,"------>"+t.toString());
                onloadedError();
            }
        });
    }

    private void onloadedError() {
        if (mViewCallback != null) {
            mViewCallback.onError();
        }
    }

    @Override
    public void getContentByCategory(SelectedPageCategory.DataBean item) {

        int categoryId=item.getFavorites_id();
        LogUtils.d(SelectedPagePresenterImpl.this,"SelectedPagePresenterImpl"+categoryId);
        String targetUrl = UrlUtils.getSelectedPageContentUrl(categoryId);
        LogUtils.d(SelectedPagePresenterImpl.this,"SelectedPagePresenterImpl"+targetUrl);
        Call<SelectContent> task = mApi.getSelectedPageContent(targetUrl);
        task.enqueue(new Callback<SelectContent>() {
            @Override
            public void onResponse(Call<SelectContent> call, Response<SelectContent> response) {
                int code = response.code();
                if(code== HttpURLConnection.HTTP_OK) {
                    SelectContent result = response.body();
                    LogUtils.d(SelectedPagePresenterImpl.this,"SelectedPagePresenterImpl"+result);
                    if(mViewCallback!=null){
                        mViewCallback.onContentLoad(result);
                    }
                }else {
                    onloadedError();
                }
            }
            @Override
            public void onFailure(Call<SelectContent> call, Throwable t) {
                onloadedError();
            }
        });
    }

    @Override
    public void reloadContent() {
            getCategories();
    }

    //只有一个界面只保存一个接口，不用集合
    @Override
    public void registerViewCallback(ISelectPageCallback callback) {
         this.mViewCallback=callback;
    }

    @Override
    public void unregisterViewCallback(ISelectPageCallback callback) {
        this.mViewCallback=null;
    }
}
